import { defineStore } from "pinia";
import pinia from ".";
import variable from '@/styles/variable.module.scss'
import { pascalToKebab } from "@/utils"

type Types = 'info' | 'primary' | 'success' | 'warning' | 'danger' | 'disabled'
type ThemeConfig = {
  [K in Types]: Record<'textColor' 
    | 'backgroundColor' 
    | 'shadowColor' 
    | 'borderColor'
    | 'strokeColor', string>
}
type ExtractThemeKey<T> = {
  [K in keyof T as K extends `${string}theme${string}` ? K : never]: T[K]
} extends infer R ? T extends R ? T[keyof R] : never : never
const obj: Record<ExtractThemeKey<typeof variable>, ThemeConfig> = {
  [variable.theme1]: {
    info: {
      backgroundColor: '#205684',
      shadowColor: '#030001',
      borderColor: '#fff3',
      textColor: '#fff',
      strokeColor: '#030001',
    },
    primary: {
      backgroundColor: '#7ED0E8',
      shadowColor: '#3980AE',
      borderColor: '#92E2F9',
      textColor: '#FFFFFF',
      strokeColor: '#030001',
    },
    success: {
      backgroundColor: '#7CC34B',
      shadowColor: '#2E874D',
      borderColor: '#8AD953',
      textColor: '#FFFFFF',
      strokeColor: '#030001',
    },
    warning: {
      backgroundColor: '#FF9431',
      shadowColor: '#C9600F',
      borderColor: '#FFAF64',
      textColor: '#FFFFFF',
      strokeColor: '#030001',
    },
    danger: {
      textColor: '',
      backgroundColor: '',
      shadowColor: '',
      borderColor: '',
      strokeColor: '',
    },
    disabled: {
      backgroundColor: '#999999',
      shadowColor: '#737171',
      borderColor: '#B8B8B8',
      textColor: '#D9DADB',
      strokeColor: '#191919',
    }
  },
  [variable.theme2]: {

  }
}

export default defineStore('theme', {
  state() {
    const arr = Object.keys(obj)

    return {
      /** 主题 */
      themeType: arr[0],
      /** 主题列表 */
      themeList: arr
    }
  },
  getters: {
    variable(state) {
      const { themeType } = state
      const config = obj[themeType]
      type Config = typeof config
      type UnionToIntersection<U> =
        (U extends any ? (x: U) => any : never) extends (x: infer I) => any ? I : never;
      type Flatten<T> =
        UnionToIntersection<{
          [K in keyof T]: {
            [P in keyof T[K]as `${K & string}${Capitalize<P & string>}`]: T[K][P]
          }
        }[keyof T]>
      return Object.entries(config).reduce((p, c) => {
        const [type, colors] = c
        for (const [colorKey, color] of Object.entries(colors)) {
          const key = (colorKey.slice(0, 1).toUpperCase()
            + colorKey.slice(1)) as Capitalize<typeof colorKey>
          Reflect.set(p, type + key, color)
        }
        return p
      }, <Flatten<Config>>{})
    },
  },
  actions: {
    styleVariable() {
      type Config = typeof this.variable
      type ConfigKeys = keyof Config
      type Result = { [K in ConfigKeys as `--${PascalToKebab<K>}`]: Config[K] }

      return Object.entries(this.variable).reduce((p, c) => {
        const [key, value] = c

        Reflect.set(p, '--' + pascalToKebab(key), value)
        return p
      }, <Result>{})
    }
  }
})(pinia)